import java.util.ArrayList;
import java.util.Scanner;

public class Studentsystem {
    public static void startSystem() {
        ArrayList<Student> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        loop:
        while (true) {
            System.out.println("--------------学生管理界面-------------");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5:退出");
            System.out.println("请选择:");
            int choose = sc.nextInt();
            switch (choose) {
                case 1:
                    addstudent(list);
                    break;
                case 2:
                    delstudent(list);
                    break;
                case 3:
                    updatestudent(list);
                    break;
                case 4:
                    findstudent(list);
                    break;
                case 5:
                    break loop;//跳出while  或System.exit(0);
                default:
                    System.out.println("meiyou");
                    ;
            }
        }
    }

    public static void addstudent(ArrayList<Student> list) {
        Scanner a = new Scanner(System.in);
        Student s = new Student();
        while (true) {
            String id = a.next();
            boolean flag = contain(list, id);
            if (flag) {
                System.out.println("重新输入");
            } else {
                s.setId(id);
                break;
            }
        }
        String name = a.next();
        s.setName(name);
        int age = a.nextInt();
        s.setAge(age);
        String address = a.next();
        s.setAddress(address);
        list.add(s);
        System.out.println("添加成功");

    }

    public static void delstudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删的id");
        String id = sc.next();
        int a = index(list, id);
        if (a < 0) {
            System.out.println("id不存在");
        } else {
            list.remove(a);
            System.out.println("成功");
        }

    }

    public static void updatestudent(ArrayList<Student> list) {

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的id");
        String id = sc.next();
        int index = index(list, id);
        if (index == -1) {
            System.out.println("不存在");
            return;
        }
        //获取要修改学生的对象
        Student stu = list.get(index);
        System.out.println("请输入新的姓名");
        String newname = sc.next();
        stu.setName(newname);
        System.out.println("请输入新的年龄");
        int newage = sc.nextInt();
        stu.setAge(newage);
        System.out.println("请输入新的地址");
        String newaddress = sc.next();
        stu.setAddress(newaddress);

    }

    public static void findstudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("无学生信息，请先添加");
            return;//结束方法
        } else {
            System.out.println("Id\t姓名\t年龄\t家庭住址");
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).getId() + "     " + list.get(i).getName() + "   " + list.get(i).getAge() + "       " + list.get(i).getAddress());
            }
        }
    }

    public static boolean contain(ArrayList<Student> list, String newid) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(newid)) {
                System.out.println("id 已经存在");
                return true;
            }
        }
        return false;
        //return index(list,id)>=0;
    }

    public static int index(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }
}


